package com.yeyks.controller.member;

import com.google.common.collect.Lists;
import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.annotation.Login;
import com.yeyks.annotation.NeedToken;
import com.yeyks.annotation.SystemLog;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.annotation.SysLog;
import com.yeyks.common.base.Arguments;
import com.yeyks.common.base.PageResult;
import com.yeyks.common.constants.AccountType;
import com.yeyks.common.constants.SysLogMsg;
import com.yeyks.common.dto.member.MemberCardSelectServerAllDTO;
import com.yeyks.common.em.accountinfo.AccountInfoTypeEnum;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.param.member.*;
import com.yeyks.common.result.member.MemberCardHelpPayListResult;
import com.yeyks.commonReference.service.account.AccountInfoService;
import com.yeyks.commonReference.service.consumer.MemberCardService;
import com.yeyks.commonReference.service.hotel.HotelInfoService;
import com.yeyks.hotel.dal.domain.HotelInfo;
import com.yeyks.web.AccountUtils;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "会员卡管理")
@RestController
@RequestMapping("/ajax/memberCard")
public class MemberCardController {

    @Autowired
    private MemberCardService memberCardService;
    @Autowired
    private AccountInfoService accountService;
    @Autowired
    private HotelInfoService hotelService;

    @ApiOperation(value = "vip卡酒店管理页面总接口", notes = "vip卡酒店管理页面总接口")
    @RequestMapping(value = "/selectForHotelManagerPage", method = RequestMethod.POST)
    public AjaxResponseBody selectForHotelManagerPage(MemberCardQueryParam memberCardQueryParam) {
        String token = memberCardQueryParam.getToken();
        if (StringUtils.isBlank(token)) {
            throw new ServiceException("token不存在");
        }
        //account信息
        AccountInfo accountInfo = null;
        try {
            accountInfo = accountService.token2AccountInfo(token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (null == accountInfo) {
            throw new ServiceException("用户信息错误");
        }
        //如果是子账户就使用父账户的id进行
        Integer accountId = accountInfo.getId();
        if (null != accountInfo.getManagerId()) {
            accountId = accountInfo.getManagerId();
        }
        List<HotelInfo> hotelInfos = hotelService.searchHotelsByOperator(accountId);
        if (null == hotelInfos || hotelInfos.size() < 1) {
            throw new ServiceException("用户信息错误");
        }
        memberCardQueryParam.setHotelId(hotelInfos.get(0).getId());
        //臻选
        memberCardQueryParam.setCardType(1);
        return memberCardService.selectForHotelManagerPage(memberCardQueryParam);
    }

    @Login
    @ApiOperation(value = "查询包含运营商和服务商【尊享】vip卡", notes = "查询包含运营商和服务商的vip卡")
    @RequestMapping(value = "/selectServerAll", method = RequestMethod.POST)
    public AjaxResponseBody selectServerAll(MemberCardServerQueryParam memberCardQueryParam) {
        AccountInfo loginUser = AccountUtils.getLoginUser();
        return memberCardService.selectServerAll(memberCardQueryParam, loginUser);
    }

    @ApiOperation(value = "批量绑定运营商，销售商【尊享】vip卡", notes = "批量绑定运营商，销售商vip卡")
    @RequestMapping(value = "/bindServerVipCard", method = RequestMethod.POST)
    public AjaxResponseBody bindServerVipCard(@Valid MemberCardBindParam memberCardBindParam) {

        return memberCardService.bindServerVipCard(memberCardBindParam);
    }

    @NeedToken
    @ApiOperation(value = "V4.3（更新了是否代卖字段) 分页查询【优享】VIP卡", notes = "分页查询【优享】VIP卡接口信息", response = MemberCardSelectServerAllDTO.class)
    @RequestMapping(value = "/selectMinVipCard", method = RequestMethod.POST)
    public AjaxResponseBody selectMinVipCard(MinMemberCardQueryParam param) {

        AccountInfo loginUser = AccountUtils.getLoginUser();
        return memberCardService.selectMinVipCard(param, loginUser);
    }

    @NeedToken
    @ApiOperation(value = "批量绑定【优享】VIP卡（3.6.1）", notes = "批量绑定【优享】VIP卡接口信息")
    @RequestMapping(value = "/bindMinVipCard", method = RequestMethod.POST)
    public AjaxResponseBody bindMinVipCard(@Valid MinMemberCardBindParam param) {

        return memberCardService.bindMinVipCard(param);
    }

    @NeedToken
    @ApiOperation(value = "V4.1.0 批量划拨【优享】VIP卡给卡商", notes = "V4.1.0 批量划拨【优享】VIP卡给卡商")
    @RequestMapping(value = "/bindMinVipCardBusiness", method = RequestMethod.POST)
    public AjaxResponseBody bindMinVipCardBusiness(@Valid MinMemberCardBusinessBindParam param) {

        AccountInfo loginUser = AccountUtils.getLoginUser();
        return memberCardService.bindMinVipCardBusiness(param, loginUser);
    }

    @ApiOperation(value = "查询vip卡", notes = "查询vip卡")
    @RequestMapping(value = "/selectAll", method = RequestMethod.POST)
    public AjaxResponseBody selectAll(MemberCardQueryParam memberCardQueryParam) {
        return memberCardService.selectAll(memberCardQueryParam);
    }

    @ApiOperation(value = "批量绑定vip卡", notes = "批量绑定vip卡")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "hotelId", value = "酒店id", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "startCardId", value = "起始vip卡id", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "endCardId", value = "截止vip卡id", dataType = "String"),
    })
    @RequestMapping(value = "/bindVipCard", method = RequestMethod.POST)
    public AjaxResponseBody bindVipCard(Integer hotelId, String startCardId, String endCardId) {
        return memberCardService.bindVipCard(hotelId, startCardId, endCardId);
    }

    @ApiOperation(value = "批量解绑vip卡", notes = "批量解绑vip卡")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "startCardId", value = "起始vip卡id", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "endCardId", value = "截止vip卡id", dataType = "String"),
    })
    @RequestMapping(value = "/unBindVipCard", method = RequestMethod.POST)
    public AjaxResponseBody unBindVipCard(String startCardId, String endCardId) {
        return memberCardService.unBindVipCard(startCardId, endCardId);
    }

    @SystemLog(value = SysLogMsg.MEMBER_CARD_WEB_PAY)
    @NeedToken
    @ApiOperation(value = "v3.8.1 --> web端充值")
    @RequestMapping(value = "/web/recharge", method = RequestMethod.POST)
    public AjaxResponseBody<Boolean> webRecharge(@Validated MemberCardRechargeParam param) {
        AccountInfo loginUser = AccountUtils.getLoginUser();
        Arguments.isFalse(AccountInfoTypeEnum.Administrator.value().equals(loginUser.getType()), "您无权访问");
        return AjaxResponseBody.success(memberCardService.webRecharge(param));
    }

    @NeedToken
    @ApiOperation(value = "v4.3 --> 会员卡代卖列表", response = MemberCardHelpPayListResult.class)
    @PostMapping(value = "/help/pay/list")
    public AjaxResponseBody<PageResult<MemberCardHelpPayListResult>> helpPayList(MemberCardHelpPayListParam param) {
        AccountInfo loginUser = AccountUtils.getLoginUser();
        Arguments.isFalse(Lists.newArrayList(AccountType.Administrator, AccountType.MinVipCardMerchant, AccountType.MinOperateMerchant).contains(loginUser.getType()), "您无权访问");
        param.setType(loginUser.getType());
        param.setId(loginUser.getId());
        PageResult<MemberCardHelpPayListResult> result = memberCardService.helpPayList(param);
        return AjaxResponseBody.success(result);
    }

    @SystemLog(value = SysLogMsg.MEMBER_CARD_HELP_PAY)
    @NeedToken
    @ApiOperation(value = "v4.3 --> 会员卡代卖")
    @PostMapping(value = "/help/pay")
    public AjaxResponseBody helpPay(String cardNo) {
        Arguments.strIsNull(cardNo, "会员卡号不能为空");
        AccountInfo loginUser = AccountUtils.getLoginUser();
        Arguments.isFalse(Lists.newArrayList(AccountType.MinVipCardMerchant, AccountType.MinOperateMerchant).contains(loginUser.getType()), "您无权访问");
        Boolean flag = memberCardService.helpPay(cardNo, loginUser);
        return AjaxResponseBody.success(flag);
    }

    @SystemLog(value = SysLogMsg.MEMBER_CARD_HELP_PAY)
    @NeedToken
    @ApiOperation(value = "v4.3 --> [运营商] 会员卡代卖")
    @PostMapping(value = "/help/business/pay")
    public AjaxResponseBody helpBusinessPay(String startCardNo, String endCardNo){
        Arguments.strIsNull(startCardNo,  "起始卡号不能为空");
        Arguments.strIsNull(endCardNo, "结束卡号不能为空");
        AccountInfo loginUser = AccountUtils.getLoginUser();
        Arguments.isFalse(Lists.newArrayList(AccountType.MinVipCardMerchant, AccountType.MinOperateMerchant).contains(loginUser.getType()), "您无权访问");
        Boolean flag = memberCardService.helpBusinessPay(startCardNo, endCardNo , loginUser);
        return AjaxResponseBody.success(flag);
    }

    @NeedToken
    @ApiOperation("V4.3 -> 随机获取该[运营商/卡商]下的一张激活会员卡")
    @PostMapping("/get/random/card")
    public AjaxResponseBody<String> getRandomCardNoByAccount(){
        AccountInfo loginUser = AccountUtils.getLoginUser();
        Arguments.isFalse(Lists.newArrayList(AccountType.MinVipCardMerchant, AccountType.MinOperateMerchant).contains(loginUser.getType()), "您无权访问");
        String cardNo = memberCardService.getRandomCardNoByAccount(loginUser);
        return AjaxResponseBody.success(cardNo);
    }

}
